
import wx
import win32con

import const as CONST
from pop_panel import PopupFrame

class CText(wx.TextCtrl):
    def __init__(self, parent):
        self.parent = parent
        wx.TextCtrl.__init__(self, parent, -1, size=parent.wh, style=wx.TE_PROCESS_ENTER)
        self.setBindings()
        self.SetBackgroundColour(CONST.CONS_COLOR)
        
        self.SetFont(wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
        
    def setBindings(self):
        self.Bind(wx.EVT_LEFT_DOWN, self.parent.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.parent.OnLeftUp)
        self.Bind(wx.EVT_MOTION, self.parent.OnMouseMove)
        self.Bind(wx.EVT_TEXT, self.parent.onText)
        self.Bind(wx.EVT_KEY_DOWN, self.parent.onKeyDown )


class CFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,  style=wx.FRAME_SHAPED)
        
        self.RegisterHotKey(100, win32con.MOD_CONTROL, win32con.VK_F2)
        self.setBindings()
        self.setSize()
        
        self.txt = CText(self)
        self.txt.Show(True)
        self.MoveXY(self.sX, self.sY)
        
        self.pop = PopupFrame(self)
        self.setDefsize()
        
        self.SetAutoLayout(True)
        
        
        
    def setBindings(self):
        self.Bind(wx.EVT_LEFT_DOWN,     self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP,       self.OnLeftUp)
        self.Bind(wx.EVT_MOTION,        self.OnMouseMove)
        self.Bind(wx.EVT_HOTKEY, self.onHotKey, id=100)
        self.Bind(wx.EVT_KEY_DOWN, self.onKeyDown )
        
    def setDefsize(self):
        w, h = self.wh
        self.SetShape(wx.Region(0, 0, w, h))
        
    def setSize(self):
        ds = wx.GetDisplaySize()
        self.SetSize(ds)
        x, y = ds
        self.sY = int(y*0.40)
        self.sX = int(x*0.15)
        w = int(x*0.7)
        h = 30
        self.pos = (self.sX, self.sY)
        self.wh = (w, h)
        self.region = wx.Region(0, 0, w, h)
        self.SetShape(self.region)
        
    def onHotKey(self, evt):
        print 'hot key'
        self.Raise()
        
    def onKeyDown(self, evt):
        if evt.GetKeyCode() == wx.WXK_ESCAPE:
            self.pop.pop(False)
            self.setDefsize()
            print 'esc'
        evt.Skip()
        
    def OnLeftDown(self, evt):
        self.CaptureMouse()
        x, y = self.ClientToScreen(evt.GetPosition())
        originx, originy = self.GetPosition()
        dx = x - originx
        dy = y - originy
        self.delta = ((dx, dy))
        
    def OnLeftUp(self, evt):
        if self.HasCapture():
            self.ReleaseMouse()
    
    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.ClientToScreen(evt.GetPosition())
            fp = (x - self.delta[0], y - self.delta[1])
            self.Move(fp)
            
    def onText(self, evt):
        if evt.GetString() == 'm':
            print 'we got m'
            self.pop.setSize()
            self.pop.pop(True)
            self.Refresh()
        evt.Skip()



        



